package com.hnkywl.sts.dao.sys;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.sys.Zdkhz;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 说明：
 * 包:        com.hnkywl.sts.dao.sys
 * 类描述:    控货价dao类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Repository
public class ZdkhzDao extends SimpleHibernateTemplate<Zdkhz, Long> {

    public Page<Zdkhz> findAllZdkhzList(String zdkhzKh, Date start, Date endDate, int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Zdkhz> page = new Page<Zdkhz>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (start != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" dateCreated >= ? ");
            param.add(start);
        }
        if (endDate != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" dateCreated < ? ");
            param.add(endDate);
        }
        if (zdkhzKh != null && !zdkhzKh.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" zyf.qc like ? ");
            param.add("%" + zdkhzKh.trim() + "%");
        }
        //==============记录数量===============
        hql = "select count(*) from Zdkhz where deleted != 1  " + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Zdkhz where deleted != 1 " + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        hql = hql + " order by drrq desc ";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    public List<Map> findExportQyjgZdkhz() {
        String sql = "select g.qc as qc ,g.jc as jc ,zd.zdkhz as zdkhz " +
                " from t_sys_gsjg g left join  t_sys_zdkhz zd on g.id =zd.zyfid " +
                " where g.type = 'zyf' and (g.lskh = 0 or g.lskh is null) and g.deleted = 0 " +
                " and (zd.deleted = 0 or zd.deleted is null) and zd.zdkhz is not null ";
        return (List<Map>) createSQLQuery(sql).setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE).list();
    }

    public Zdkhz findByZyfId(Long zyfId) {
        String selHql = " from Zdkhz where zyf.id = ? ";
        List<Zdkhz> zdkhzList = this.find(selHql, zyfId);
        return CollectionUtils.isEmpty(zdkhzList) ? null : zdkhzList.get(0);
    }
}
